﻿Public Class CustomKOTabelleEditor
    Public Enum Modi
        OffenceTable
        DeffenceTable
        MoveTable
    End Enum
    Dim Mode As Modi
    Dim Writer As frmScriptWriter
    Public Sub New()

        ' Dieser Aufruf ist fuer den Windows Form-Designer erforderlich.
        InitializeComponent()

        ' Fuegen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        'Waffentabelle
        dgvCuWaffe.Rows.Clear()
        For I = 0 To frmScriptWriter.UnitUebersetzt.Length - 1
            Dim Z As DataGridViewTextBoxCell = dgvCuWaffe.Rows(dgvCuWaffe.Rows.Add()).Cells(SpalteCuWaffeOpfer.Index)
            Z.Value = frmScriptWriter.UnitUebersetzt(I)
            Z.ReadOnly = True
            Z = dgvCuWaffe.Rows(Z.RowIndex).Cells(SpalteCuDamage.Index)
            Z.Value = "100"
        Next
        cbMovement.Items.Clear()
        cbMovement.Items.AddRange(frmScriptWriter.BewegungsartUebersetzt)
        cbMovement.SelectedIndex = 0
        Me.Visible = False
    End Sub

    Public Sub Ini(ByVal ScriptWriter As frmScriptWriter, ByVal Mode As Modi)
        If Mode = Modi.MoveTable Then
            For I = 0 To frmScriptWriter.UnitUebersetzt.Length - 1
                Dim Z As DataGridViewTextBoxCell = dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index)
                Z.Value = "0"
            Next
        End If
        ScriptWriter.Visible = False
        dgvCuWaffe.Visible = False
        Me.Mode = Mode
        Writer = ScriptWriter
        Me.Visible = True
        Select Case Mode
            Case Modi.OffenceTable
                dgvCuWaffe.Visible = True
                If TypeOf ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).OffensivStaerke Is AngriffstabelleCustom Then
                    Using Writer As New IO.StreamWriter(DataSettings.Resourcenpfad + "Table.tmp", False, Hauptmenue.Codierung)
                        CType(ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).OffensivStaerke, AngriffstabelleCustom).Write(Writer)
                    End Using
                    Using StrR As New IO.StreamReader(DataSettings.Resourcenpfad + "Table.tmp", Hauptmenue.Codierung)
                        While Not StrR.EndOfStream
                            While Not StrR.EndOfStream
                                Dim Z1() As String = StrR.ReadLine.Trim.Split(":")
                                If Z1(0).ToLower.StartsWith("tabellenende") Then Exit While
                                For I = 0 To dgvCuWaffe.Rows.Count - 2
                                    If frmScriptWriter.getEinheitenID(dgvCuWaffe.Rows(I).Cells(SpalteCuWaffeOpfer.Index).Value).ToLower = Z1(0).ToLower Then
                                        dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index).Value = Z1(1)
                                        Continue While
                                    ElseIf I = dgvCuWaffe.Rows.Count - 2 Then
                                        Dim Zelle As DataGridViewTextBoxCell = dgvCuWaffe.Rows(dgvCuWaffe.Rows.Add()).Cells(SpalteCuWaffeOpfer.Index)
                                        Zelle.Value = Z1(0)
                                        Zelle = dgvCuWaffe.Rows(Zelle.RowIndex).Cells(SpalteCuDamage.Index)
                                        Zelle.Value = Z1(1)
                                    End If
                                Next
                            End While
                        End While
                    End Using
                    My.Computer.FileSystem.DeleteFile(DataSettings.Resourcenpfad + "Table.tmp")
                End If
            Case Modi.DeffenceTable
                dgvCuWaffe.Visible = True
                If TypeOf ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).DeffensivStaerke Is AngriffstabelleCustom Then
                    Using Writer As New IO.StreamWriter(DataSettings.Resourcenpfad + "Table.tmp", False, Hauptmenue.Codierung)
                        CType(ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).DeffensivStaerke, AngriffstabelleCustom).Write(Writer)
                    End Using
                    Using StrR As New IO.StreamReader(DataSettings.Resourcenpfad + "Table.tmp", Hauptmenue.Codierung)
                        While Not StrR.EndOfStream
                            While Not StrR.EndOfStream
                                Dim Z1() As String = StrR.ReadLine.Trim.Split(":")
                                If Z1(0).ToLower.StartsWith("tabellenende") Then Exit While
                                For I = 0 To dgvCuWaffe.Rows.Count - 2
                                    If frmScriptWriter.getEinheitenID(dgvCuWaffe.Rows(I).Cells(SpalteCuWaffeOpfer.Index).Value).ToLower = Z1(0).ToLower Then
                                        dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index).Value = Z1(1)
                                        Continue While
                                    ElseIf I = dgvCuWaffe.Rows.Count - 2 Then
                                        Dim Zelle As DataGridViewTextBoxCell = dgvCuWaffe.Rows(dgvCuWaffe.Rows.Add()).Cells(SpalteCuWaffeOpfer.Index)
                                        Zelle.Value = Z1(0)
                                        Zelle = dgvCuWaffe.Rows(Zelle.RowIndex).Cells(SpalteCuDamage.Index)
                                        Zelle.Value = Z1(1)
                                    End If
                                Next
                            End While
                        End While
                    End Using
                    My.Computer.FileSystem.DeleteFile(DataSettings.Resourcenpfad + "Table.tmp")
                End If
            Case Modi.MoveTable
                dgvCuWaffe.Visible = True
                If TypeOf ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).Bewegungsbonus Is BewegungsboniTabelleCustom Then
                    Using Writer As New IO.StreamWriter(DataSettings.Resourcenpfad + "Table.tmp", False, Hauptmenue.Codierung)
                        CType(ScriptWriter.CustomCOPowers(ScriptWriter.cbCustomCOPower.SelectedIndex).Bewegungsbonus, BewegungsboniTabelleCustom).Write(Writer)
                    End Using
                    Using StrR As New IO.StreamReader(DataSettings.Resourcenpfad + "Table.tmp", Hauptmenue.Codierung)
                        While Not StrR.EndOfStream
                            While Not StrR.EndOfStream
                                Dim Z1() As String = StrR.ReadLine.Trim.Split(":")
                                If Z1(0).ToLower.StartsWith("tabellenende") Then Exit While
                                For I = 0 To dgvCuWaffe.Rows.Count - 2
                                    If frmScriptWriter.getEinheitenID(dgvCuWaffe.Rows(I).Cells(SpalteCuWaffeOpfer.Index).Value).ToLower = Z1(0).ToLower Then
                                        dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index).Value = Z1(1)
                                        Continue While
                                    ElseIf I = dgvCuWaffe.Rows.Count - 2 Then
                                        Dim Zelle As DataGridViewTextBoxCell = dgvCuWaffe.Rows(dgvCuWaffe.Rows.Add()).Cells(SpalteCuWaffeOpfer.Index)
                                        Zelle.Value = Z1(0)
                                        Zelle = dgvCuWaffe.Rows(Zelle.RowIndex).Cells(SpalteCuDamage.Index)
                                        Zelle.Value = Z1(1)
                                    End If
                                Next
                            End While
                        End While
                    End Using
                    My.Computer.FileSystem.DeleteFile(DataSettings.Resourcenpfad + "Table.tmp")
                End If
        End Select
    End Sub

    Private Sub CustomKOTabelleEditor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Writer.Visible = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using StrW As New IO.StreamWriter(DataSettings.Resourcenpfad + "Table.tmp", False, Hauptmenue.Codierung)
            For I = 0 To dgvCuWaffe.Rows.Count - 2
                Dim Wert As Integer = dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index).Value
                Dim Name As String = frmScriptWriter.getEinheitenID(dgvCuWaffe.Rows(I).Cells(SpalteCuWaffeOpfer.Index).Value)
                If Name = "Building" Then Name = "Gebaeude"
                If Wert >= 0 Then
                    StrW.WriteLine(Name + ":" + Wert.ToString)
                End If
            Next
            StrW.WriteLine("Tabellenende")
        End Using
        If Mode = Modi.MoveTable Then
            Dim Table As BewegungsboniTabelleCustom
            Using Reader As New IO.StreamReader(DataSettings.Resourcenpfad + "Table.tmp", Hauptmenue.Codierung)
                Table = New BewegungsboniTabelleCustom(Reader)
            End Using
            Writer.CustomCOPowers(Writer.cbCustomCOPower.SelectedIndex).Bewegungsbonus = Table
            My.Computer.FileSystem.DeleteFile(DataSettings.Resourcenpfad + "Table.tmp")
        Else
            Dim Table As AngriffstabelleCustom
            Using Reader As New IO.StreamReader(DataSettings.Resourcenpfad + "Table.tmp", Hauptmenue.Codierung)
                Table = New AngriffstabelleCustom(Reader)
            End Using
            Select Case Mode
                Case Modi.OffenceTable
                    Writer.CustomCOPowers(Writer.cbCustomCOPower.SelectedIndex).OffensivStaerke = Table
                Case Modi.DeffenceTable
                    Writer.CustomCOPowers(Writer.cbCustomCOPower.SelectedIndex).DeffensivStaerke = Table
            End Select
            My.Computer.FileSystem.DeleteFile(DataSettings.Resourcenpfad + "Table.tmp")
        End If
        Me.Close()
    End Sub

    Private Sub dgvCuWaffe_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCuWaffe.CellEndEdit
        If e.RowIndex >= 0 Then
            Dim S As String = dgvCuWaffe.Rows(e.RowIndex).Cells(SpalteCuDamage.Index).Value
            If Not IsNumeric(S) Then
                dgvCuWaffe.Rows(e.RowIndex).Cells(SpalteCuDamage.Index).Value = "100"
            End If
        End If
    End Sub

    Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim Movement As String = frmScriptWriter.getBewegungskID(cbMovement.SelectedItem)
        For I = 0 To dgvCuWaffe.Rows.Count - 2
            Dim Name As String = frmScriptWriter.getEinheitenID(dgvCuWaffe.Rows(I).Cells(SpalteCuWaffeOpfer.Index).Value)
            If ObjektLadeklasse.NewEinheit(Name, 0, 0, 0, 1).Bewegungsart = Movement Then
                dgvCuWaffe.Rows(I).Cells(SpalteCuDamage.Index).Value = nudBoost.Value
            End If
        Next
    End Sub
End Class